package mv.io;

import mv.main.MObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class description...
 *
 * @author mvu
 */
public class SimpleProperties extends MObject {
  private static Properties _properties;
  private static final String _file = "simple.properties";
  private static final String NOTEBOOK_LAST_UPDATED = "NotebookLastUpdated";
  private static final String FOLDER_LAST_UPDATED = "LastModified";
  private static final String USERNAME = "username";
  private static final String PASSWORD = "password";
  private static final String ROOT_FILE = "root.file";
  private static final String PASSCODE = "passcode";

  private SimpleProperties(){}

  public static Properties getProperties() {
    if (_properties == null) {
      loadProperties();
    }
    return _properties;
  }

  public static void loadProperties() {
    File file = new File(_file);
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      FileInputStream in = new FileInputStream(file);
      _properties = new Properties();
      _properties.load(in);
      in.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void setLastModified(long time) throws IOException {
    getProperties().setProperty(FOLDER_LAST_UPDATED, String.valueOf(time));
    saveProperties();
  }

  private static void saveProperties(){
    try {
      FileOutputStream out = new FileOutputStream(_file);
      getProperties().store(out, "---No Comment---");
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static long getLastModified() throws IOException {
    String lastModified = getProperties().getProperty(FOLDER_LAST_UPDATED, "0");
    return Long.valueOf(lastModified);
  }

  public static long getNotebookLastUpdated() {
    String lastModified = getProperties().getProperty(NOTEBOOK_LAST_UPDATED, "0");
    return Long.valueOf(lastModified);
  }

  public void setNotebookLastUpdated(long time) {
    getProperties().setProperty(NOTEBOOK_LAST_UPDATED, String.valueOf(time));
    try {
      FileOutputStream out = new FileOutputStream(_file);
      getProperties().store(out, "---No Comment---");
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static String getUsername() {
    return getProperty(USERNAME, null, false);
  }

  private static String getProperty(String name, String defaultValue, boolean nullOk){
    String value = getProperties().getProperty(name, defaultValue);
    if(value == null && !nullOk){
      value = getInput(name);
      getProperties().setProperty(name, value);
      saveProperties();
    }
    return value;
  }

  public static String getPassword() {
    String password = getProperty(PASSWORD, null, true);
    if(password == null){
      password = getInput(PASSWORD);
      getProperties().setProperty(PASSWORD, encrypt(password, 1));
      saveProperties();
    }
    return encrypt(password, -1);
  }

  private static String encrypt(String password, int key) {
    char[] values = password.toCharArray();
    for (int i = 0; i < values.length; i++) {
      values[i] = (char) ((int) values[i] + key);
    }
    return String.valueOf(values);
  }

  public static String getRootFile() {
    return getProperty(ROOT_FILE, "wap", false);
  }

  public static int getPasscode() {
    return Integer.parseInt(getProperty(PASSCODE, null, false));
  }

  public static void main(String[] args) {
    getUsername();
    getPassword();
    getPasscode();
  }
}
